/* util */
import _ from 'lodash';
/* api */
import * as SettingTaskApi from '@src/api/SettingTaskApi';
import { parse } from '@src/util/querystring'
// components
import EditorColumnDialog from '../../components/EditorColumnDialog'
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import i18n from '@src/locales'
import { toast } from '@src/util/platform';

export default {
  name: 'task_card_setting',
  data() {
    return {
      pending: false,
      loading: true,
      cardData: {},
      cardConfig: {},
      fieldsList:[],
      cardType:'',
      visible: false,
      isError: false,
      isWarehouseError: false,
      isWarehouseError1: false,
      outboundOption: [{
        label: i18n.t('task.setting.materialSelect.option1'),
        value: 'CLAIMANT_ORGANIZATION'
      }, {
        label: i18n.t('task.setting.materialSelect.option2'),
        value: 'CLAIMANT_NEARBY'
      }, {
        label: i18n.t('task.setting.materialSelect.option3'),
        value: 'CUSTOMER_NEARBY '
      }, {
        label: i18n.t('task.setting.materialSelect.option4'),
        value: 'APPOINT'
      }],
      fields: [],
      warehouseOption: []
    }
  },
  computed: {
    // 编辑字段选项的字段
    fieldsOptions() {
      return this.fieldsList;
    }
  },
  mounted(){
    let query = parse(window.location.search) || {};
    this.taskCardId = query.cardId;
    this.cardType = query.cardType;
    this.initCardView();
    this.fetchWarehouseSelect();
  },
  methods: {
    // 编辑列表显示字段
    onEditColumn() {
      this.$refs.editorColumnDialog.openDialog();
    },
    // 保存编辑字段
    onSaveSetSubmit(value) {
      this.fields = value.map(item => {
        return {
          ...item,
          isHidden: 0
        }
      });
    },
    onSubmit() {
      if(this.cardConfig.isOutRep2Task && this.cardConfig.roleList && this.cardConfig.roleList.length == 0) {
        this.isError = true
        return this.$message.warning(i18n.t('task.tip.materialApplyTip1'));
      }

      if(this.cardConfig?.recommendWarehouseType?.length == 0) {
        this.isWarehouseError = true;
        return;
      }

      if(this.cardConfig?.appointRecommendWarehouse == null && this.cardConfig?.recommendWarehouseType === 'APPOINT') {
        this.isWarehouseError1 = true;
        return;
      }

      this.sardTaskCard();
    },
    // 返回
    back(){
      let fromId = window.frameElement.getAttribute('fromid');
      let id = window.frameElement.dataset.id
      this.$platform.closeTab(id);
      if(!this.cardType){
        // this.$platform.openTab({
        //   id: 'M_SYSTEM',
        //   title: '附加组件设置',
        //   url: '/setting/task/cardManage',
        //   reload: true,
        //   close: true,
        //   fromId
        // });  
        openAccurateTab({
          type: PageRoutesTypeEnum.PageTaskCardManage,
          reload: true,
          fromId
        })
      }
    },
    // 初始化组件信息
    initCardView() {
      SettingTaskApi.getInventoryConfig({taskCardId: this.taskCardId}).then(res=>{
        const { code, result } = res;
        if(code == 0){  
          this.loading = false;
          this.cardData = result;
          this.cardConfig = result.config;
          this.fieldsList = _.cloneDeep(result.taskCardFields)
          this.fields = (result?.taskCardFields || []).filter(item => item.isHidden == 0)
        }
      }).catch(error=>({}))
    },
    // 保存物料信息配置
    sardTaskCard() {
      const params = Object.assign(this.cardData, { config: this.cardConfig }, { taskCardFields: this.fields }) 
      SettingTaskApi.modifyInventoryConfig(params).then(res=>{
        const { code, message, data } = res;
        if(code == 0){
          this.$platform.notification({
            title: i18n.t('common.base.saveSuccess'),
            type: 'success'
          }); 
          setTimeout(()=>{
            this.back()
          }, 500)
        }else{
          this.$platform.notification({
            title: message,
            type: 'error'
          }); 
        }
      }).catch(error=>({}))
    },
    onPreview() {
      this.visible = true;
    },
    onClose() {
      this.visible = false;
    },
    roleListChange(value) {
      if (value && value.length > 0) {
        this.isError = false
      }
    },
    // 获取指定仓库下拉框数据
    fetchWarehouseSelect() {
      SettingTaskApi.getWarehouse().then(res=>{
        const { code, data, success, message } = res;
        if (!success) return toast(message, 'error');
        
        this.warehouseOption = data;
      }).catch(error=>({}))
    },
    // change仓库
    changeWarehouse1(val) {
      if (val && val.length > 0) {
        this.isWarehouseError = false;
      }
      this.cardConfig.appointRecommendWarehouse = null;
    },
    changeWarehouse2(val) {
      if (val && val.toString().length > 0) {
        this.isWarehouseError1 = false;
      }
    }
  },
  components: {
    [EditorColumnDialog.name]: EditorColumnDialog
  }
}